package com.foreveross.crawl.common.jpa;

import java.io.Serializable;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;

import com.foreveross.crawl.common.InstanceFactory;
import com.foreveross.crawl.common.cache.BufferCacheManager;
import com.foreveross.crawl.common.cache.IBufferCacheEntity;
import com.foreveross.crawl.common.cache.Operations;
import com.foreveross.crawl.common.domain.IBaseEntity;
/**
 * hibernateDAO基类
 * @author xiangsf wdmz433xsf@gmail.com
 * @date 2012-10-11 下午04:55:25
 * @param <T>
 * @param <PK>
 */
/*@Repository("entityHibernateRepository")*/
public class EntityHibernateRepository extends HibernateDaoSupport
			implements IBaseRepository {
	/**
	 * log4j日志记录
	 */
	protected static Logger logger = Logger.getLogger(EntityHibernateRepository.class);
	
	protected <T extends IBaseEntity> void evictCollection(T entity){
		if(logger.isDebugEnabled()){
			logger.debug("执行BaseDao里面的evictCollection");
		}
	}
	@Override
	public <T extends IBaseEntity> T get(Class<T> clazz, Serializable id) {
		return (T)super.getHibernateTemplate().get(clazz, id);
	}
	@Override
	public <T extends IBaseEntity> T find(Class<T> clazz, Serializable id) {
		return (T)super.getHibernateTemplate().load(clazz, id);
	}
	@Override
	public <T extends IBaseEntity> boolean exists(Class<T> clazz, Serializable id) {
		return get(clazz, id) != null;
	}
	@Override
	public <T extends IBaseEntity> List<T> findAll(Class<T> clazz) {
		return super.getHibernateTemplate().loadAll(clazz);
	}

	@Override
	public <T extends IBaseEntity> void create(T o) {
		
		if(o instanceof IBufferCacheEntity) {
			 BufferCacheManager bufferManger = InstanceFactory.getInstance(BufferCacheManager.class);
			 bufferManger.excure((IBufferCacheEntity)o, Operations.CREATE);
		}else {
			super.getHibernateTemplate().save(o);
			this.evictCollection(o);
		}
		
	}
	@Override
	public <T extends IBaseEntity> void update(T o) {
		if(o instanceof IBufferCacheEntity) {
			 BufferCacheManager bufferManger = InstanceFactory.getInstance(BufferCacheManager.class);
			 bufferManger.excure((IBufferCacheEntity)o, Operations.UPDATE);
		}else {
			super.getHibernateTemplate().update(o);
			this.evictCollection(o);
			
		}
		
		
	}
	@Override
	public <T extends IBaseEntity> void delete(Class<T> clazz, Serializable id) {
		this.delete(this.get(clazz, id));
	}
	@Override
	public <T extends IBaseEntity> void delete(T o) {
		if(o instanceof IBufferCacheEntity) {
			 BufferCacheManager bufferManger = InstanceFactory.getInstance(BufferCacheManager.class);
			 bufferManger.excure((IBufferCacheEntity)o, Operations.DELETE);
		}else {
			super.getHibernateTemplate().delete(o);
			this.evictCollection(o);
		}
	}
	@Override
	public <T extends IBaseEntity> void save(T o) {
		if(o.getId()!=null){
			this.update(o);
		}else {
			this.create(o);
		}
	}
	@Override
	public <T extends IBaseEntity> void create(List<T> instances) {
		for(T t : instances){
			this.create(t);
		}
	}
	@Override
	public <T extends IBaseEntity> void update(List<T> instances) {
		for(T t : instances){
			this.update(t);
		}
	}
	@Override
	public <T extends IBaseEntity> void delete(List<T> instances) {
		for(T t : instances){
			this.delete(t);
		}
	}
	@Override
	public <T extends IBaseEntity> void save(List<T> instances) {
		for(T t : instances){
			this.save(t);
		}
	}
	
	
	
}
